// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package gui.itemSetting
{
    import com.bit101.components.PushButton;

    import data.vo.LayoutItemVo;
    import data.vo.LayoutItemVoPropertyPorxy;

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.text.TextField;

    import gui.itemSetting.propertySetting.ItemPropertySettingComponent;

    import manger.LogicManger;

    import utils.ConfirmBox;
    import utils.GUIUtils;
    import utils.MessageBox;

    public class LayoutItemSettingPanel
    {
        private var mLogicManger:LogicManger;
        private var mSettingPanelMc:MovieClip;

        private var mItemVo:LayoutItemVo;

        private var mItemIDTF:TextField;
        private var mItemWidthTF:TextField;
        private var mItemHeightTF:TextField;

        private var mPropertyXSubComponent:ItemPropertySettingComponent;
        private var mPropertyYSubComponent:ItemPropertySettingComponent;

        private var mSaveButton:PushButton;
        private var mExitButton:PushButton;

        public function LayoutItemSettingPanel()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_settingPanelMc:MovieClip, _logicManger:LogicManger):void
        {
            mSettingPanelMc = _settingPanelMc;
            mLogicManger = _logicManger;

            initUIComponent();
        }

        private function initUIComponent():void
        {
            mItemIDTF = mSettingPanelMc["itemIDTF"];
            mItemWidthTF = mSettingPanelMc["itemWidthTF"];
            mItemHeightTF = mSettingPanelMc["itemHeightTF"];

            //=== X属性设置值
            mPropertyXSubComponent = new ItemPropertySettingComponent();
            mPropertyXSubComponent.initialize(mSettingPanelMc["xPropertySettingMc"],
                                              GlobalConfig.LAYOUT_ITEM_VO_PROPERTY_TYPE_X);

            //=== Y属性设置值
            mPropertyYSubComponent = new ItemPropertySettingComponent();
            mPropertyYSubComponent.initialize(mSettingPanelMc["yPropertySettingMc"],
                                              GlobalConfig.LAYOUT_ITEM_VO_PROPERTY_TYPE_Y);


            mSaveButton = new PushButton(null, 0, 0, "Save", onClickSaveBtn);
            GUIUtils.replaceTargetWithPH(mSaveButton, mSettingPanelMc["saveBtnPH"]);

            mExitButton = new PushButton(null, 0, 0, "Exit", onClickExitBtn);
            GUIUtils.replaceTargetWithPH(mExitButton, mSettingPanelMc["exitBtnPH"]);

        }

        //==================================
        //== Public Function
        //==================================

        public function hide():void {mSettingPanelMc.visible = false;}

        public function show(_vo:LayoutItemVo):void
        {
            mSettingPanelMc.visible = true;
            mItemVo = _vo;

            tryToRefreshItemBasicProperty();


            mPropertyXSubComponent.show(new LayoutItemVoPropertyPorxy(_vo,
                                                                      GlobalConfig.LAYOUT_ITEM_VO_PROPERTY_TYPE_X));
            mPropertyYSubComponent.show(new LayoutItemVoPropertyPorxy(_vo,
                                                                      GlobalConfig.LAYOUT_ITEM_VO_PROPERTY_TYPE_Y));
        }

        //==================================
        //== Private Function
        //==================================

        //=================
        //==Event Handle
        //=================

        private function onClickSaveBtn(e:Event):void
        {
            if (tryToSaveItemBasicProperty())
            {
                if (mPropertyXSubComponent.tryToSaveItemVo())
                {
                    if (mPropertyYSubComponent.tryToSaveItemVo())
                    {
                        MessageBox.instance.showMsg("保存成功");
                    }
                }
            }
        }

        private function onClickExitBtn(e:Event):void
        {
            ConfirmBox.instance.ask("是否要退出?\n未保存的数据会消失", onConfirmQuitSettingPanel);
        }

        private function onConfirmQuitSettingPanel():void
        {
            mLogicManger.onClickQuitSettingPanel();
        }


        //==================
        //== SaveData Logic
        //==================

        /**
         * 保存基础的ItemVo信息
         */
        private function tryToSaveItemBasicProperty():Boolean
        {
            if (mItemIDTF.text != "Null" && mItemIDTF.text != "")
            {
                mItemVo.itemID = mItemIDTF.text;
            } else
            {
                MessageBox.instance.showMsg("Item的ID不合法");
                return false;
            }

            if (int(mItemWidthTF.text) > 0 && int(mItemHeightTF.text) > 0)
            {
                mItemVo.itemWidth = int(mItemWidthTF.text);
                mItemVo.itemHeight = int(mItemHeightTF.text);
            }
            else
            {
                MessageBox.instance.showMsg("Item的宽或高必须大于0");
                return false;
            }

            return true;
        }


        //==================
        //== Refresh Logic
        //==================

        /**
         * 刷新基础的ItemVo信息
         */
        private function tryToRefreshItemBasicProperty():void
        {
            if (mItemVo.itemID != null)
            {
                mItemIDTF.text = mItemVo.itemID;
            }
            mItemWidthTF.text = "" + mItemVo.itemWidth;
            mItemHeightTF.text = "" + mItemVo.itemHeight;
        }


    }
}
